package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    //    根据菜单id进行查询
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catlogId) {

        // 根据key查询
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> attrGroupEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();


        if (catlogId != 0) {
            attrGroupEntityLambdaQueryWrapper.eq(AttrGroupEntity::getCatelogId, catlogId);
        }

        if(!StringUtils.isEmpty(key)) {
            attrGroupEntityLambdaQueryWrapper.and((obj) -> {
                obj.eq(AttrGroupEntity::getAttrGroupId,key).or().like(AttrGroupEntity::getAttrGroupName,key);
            });
        }

        IPage<AttrGroupEntity> page = page(new Query<AttrGroupEntity>().getPage(params), attrGroupEntityLambdaQueryWrapper);
        return new PageUtils(page);

    }

//    获取分类下所有的分组和规格属性
    @Override
    public List<AttrGroupWithAttrsVo> getGroupWithAttr(Long catelogId) {
        LambdaQueryWrapper<AttrGroupEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
        // 根据分类id查询出所有的分组
        List<AttrGroupEntity> attrGroupEntities = list(lambdaQueryWrapper);

//        if(attrGroupEntities )
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map((item) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item,attrGroupWithAttrsVo);

            // 查询每一个分组下面的所有属性
            // 我们原来写了一个根据groupId来查询我们的attr
            List<AttrEntity> attrEntities = attrService.getattrRelation(attrGroupWithAttrsVo.getAttrGroupId());

            attrGroupWithAttrsVo.setAttrs(attrEntities);


            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }


}